package com.example.xm.service.impl;

import com.example.xm.entity.Huiyuan;
import com.example.xm.entity.PinglunEntity;
import com.example.xm.entity.Xuanchuan;
import com.example.xm.entity.XuanchuanItem;
import com.example.xm.exception.SerceiceException;
import com.example.xm.mapper.HuiyuanMapper;
import com.example.xm.mapper.XuanchuanItemMapper;
import com.example.xm.mapper.XuanchuanMapper;
import com.example.xm.service.IHuiyuanService;
import com.example.xm.service.IXuanchuanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class XuanchuanService implements IXuanchuanService {
    @Autowired
    XuanchuanMapper shipinfenleiMapper;
    @Autowired
    XuanchuanItemMapper xuanchuanItemMapper;
    @Override
    public void reg(Xuanchuan fenlei) {
        fenlei.setTime(new Date());
        shipinfenleiMapper.insert(fenlei);
    }

    @Override
    public PageInfo<Xuanchuan> list(String title,Integer page,Integer size) {
        PageHelper.startPage(page,size);
        List<Xuanchuan> adminList=  shipinfenleiMapper.selectList(title);
        //查询所有item
          List<XuanchuanItem> allItemList=  xuanchuanItemMapper.selectByXid();
        for (Xuanchuan xuanchuan : adminList) {
            //该学习下的所有item
            List<XuanchuanItem> collect1 = allItemList.stream().filter(v -> v.getXid() == xuanchuan.getId()).collect(Collectors.toList());
            if(collect1.size()>0){
                List<XuanchuanItem> yiList = collect1.stream().filter(v -> v.getPid() == 0).collect(Collectors.toList());
                for (XuanchuanItem entity : yiList) {
                    List<XuanchuanItem> collect = collect1.stream().filter(v -> v.getPid() == entity.getId()).collect(Collectors.toList());
                    entity.setList(collect);
                }
                xuanchuan.setList(yiList);
            }

        }
        PageInfo<Xuanchuan> adminPageInfo = new PageInfo<Xuanchuan>(adminList);
        return adminPageInfo;
    }

    @Override
    public void update(Xuanchuan gonggao) {
        shipinfenleiMapper.updateByPrimaryKeySelective(gonggao);
    }

    @Override
    public void delete(int[] intArray) {
        shipinfenleiMapper.delete(intArray);
    }
}
